package com.cc.cbdb.common.report;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.cc.cbdb.activity.HomeActivity;
import com.cc.cbdb.common.utils.StringUtil;
import com.cc.cbdb.entity.ReportData;

import java.util.ArrayList;
import java.util.List;

/**
 * 埋点统计类
 * Created by Administrator on 2018/5/19.
 */

public class ReportHelper {

    private static final String TAG = "ReportHelper";
    
    //历史框搜索记录
    public static final String PERSONNAME = "person_names";//人物姓名
    public static final String ETHNICITY = "ethnicity";//种族、部族
    public static final String INSTITUTION = "institution";//社会机构
    public static final String STATUSCODE = "statusCode";//社会区分
    public static final String KINSHIP_SEARCH = "kinship";//亲属关系
    //埋点
    public static final String PERSONINFO = "q_personInfo";//所有人物信息
    public static final String ADDRESS = "q_address";//地址
    public static final String ADDRTYPE = "q_addrType";//地址类别
    public static final String OFFICE = "q_office";//官名
    public static final String ENTRYTYPE = "q_entryType";//入仕途径
    public static final String CHORONYM = "q_choronym";//郡望
    public static final String DY = "q_dy";//朝代
    public static final String ENTHNICITY = "q_enthnicity";//种族、部族
    public static final String APPOINTMENT = "q_appointment";//官职除授
    public static final String INST = "q_inst";//社会机构
    public static final String BIOGINST = "q_biogInst";//社交角色
    public static final String STATUS = "q_status";//社会区分
    public static final String KINSHIP = "q_kinship";//亲属关系
    public static final String ASSOC = "q_assoc";//社会关系
    public static final String CAREERCHANGE = "q_careerchange";//仕途变迁
    public static final String TEXT = "q_text";//著作

    public static final int TABTYPE1 = 1;
    public static final int TABTYPE2 = 2;

    private String tabName = null;
    public static ReportHelper reportHelper;
    private RecordSQLiteOpenHelper helper;

    private ReportHelper(Context context){
        helper = new RecordSQLiteOpenHelper(context);
    };

    public static ReportHelper getInstance(Context context){
        if (reportHelper == null)
            reportHelper = new ReportHelper(context);
        return reportHelper;
    }

    /**
     * 查询统计上报
     * @param reportType
     * @param reportData1
     * @param reportData2
     */
    public void reportQueryState(String reportType,String reportData1,String reportData2){
        if (HomeActivity.isNoRecommed) {
            HomeActivity.isNoRecommed = false;
        }
        if (reportType.equals(ReportHelper.PERSONINFO) && HomeActivity.isNoPersonRD) {
            HomeActivity.isNoPersonRD = false;
            HomeActivity.isNoRelation = false;
        }
        if (StringUtil.isNotNull(reportData1)) {
            tabName = reportType;
            String[] split = reportData1.split(",");
            for (int i = 0; i < split.length; i++) {
                ReportData queryData = queryData(split[i],reportData2);

                Log.d(TAG, "reportQueryState: "+queryData);

                if (StringUtil.isEmpty(queryData)) {
                    insertData(split[i],reportData2);
                }else {
                    updateData(queryData.getTimes()+1,split[i],reportData2);
                }
            }
        }
    }

    /**
     * 获取埋点数据，倒序
     * @param tabName
     * @param type
     * @return
     */
    public List<ReportData> getDataDesc(String tabName, int type){
        Cursor cursor;
        cursor = helper.getReadableDatabase().rawQuery(
                "select * from "+ tabName +" order by times desc", null);
        List<ReportData> reportDataList = new ArrayList<>();
        while (cursor.moveToNext()) {
            ReportData reportData = new ReportData();
            String id = cursor.getString(0);
            reportData.setId(id);
            if (type == TABTYPE1) {
                int times = cursor.getInt(1);
                reportData.setTimes(times);
            }else if (type == TABTYPE2){
                String tempid = cursor.getString(1);
                int times = cursor.getInt(2);
                reportData.setTimes(times);
                reportData.setName(tempid);
            }
            reportData.setTabName(tabName);
            reportDataList.add(reportData);
        }
        return reportDataList;
    }

    private ReportData queryData(String condition1,String condition2) {
        Cursor cursor;
        if (StringUtil.isEmpty(condition2)) {
            cursor = helper.getReadableDatabase().rawQuery(
                    "select * from "+tabName+" where id =?", new String[]{condition1});
        }else {
            cursor = helper.getReadableDatabase().rawQuery(
                    "select * from "+tabName+" where id =? and tempid =?", new String[]{condition1,condition2});
        }
        ReportData reportData = null;
        while (cursor.moveToNext()) {
            String id = cursor.getString(0);
            if (StringUtil.isEmpty(condition2)) {
                int times = cursor.getInt(1);
                reportData = new ReportData(id,times);
            }else {
                String tempid = cursor.getString(1);
                int times = cursor.getInt(2);
                reportData = new ReportData(id,tempid,times);
            }
        }
        return reportData;
    }

    private void insertData(String data1,String data2) {
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql;
        if (StringUtil.isEmpty(data2)) {
            sql = "insert into "+tabName+"(id) values('" + data1 + "')";
        }else {
            sql = "insert into "+tabName+"(id,tempid) values('" + data1 + "','"+ data2 + "')";
        }

        Log.d(TAG, "insertData: "+sql);

        db.execSQL(sql);
        db.close();
    }

    private void updateData(Integer newData,String condition1,String condition2){
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql;
        if (StringUtil.isEmpty(condition2)) {
            sql = "update "+tabName+" set times="+newData+" where id="+condition1;
        }else {
            sql = "update "+tabName+" set times="+newData+" where id="+condition1+" and tempid="+condition2;
        }

        Log.d(TAG, "updateData: "+sql);

        db.execSQL(sql);
        db.close();
    }

}
